"""
活动数据检查类，将规划中有效数据和基础表中的有效数据进行比对
"""
import copy
from TestToolsV2.common.ActivityDatas import activityDatas
from TestToolsV2.tools.Log import Log

log = Log()


class checkDatas:

    def __init__(self, _activityDatas: activityDatas):
        self.activityDatas = _activityDatas

    def PrintData(self):
        log.Show("----------------------checkDatas----------------------")
        log.Show(self.activityDatas.colorKey, 20, "colorKey")
        log.Show(self.activityDatas.reRewards, 20, "reRewards")
        log.Show(self.activityDatas.taRewards, 20, "taRewards")
        log.Show(self.activityDatas.reCosts, 20, "reCosts")
        log.Show(self.activityDatas.taCosts, 20, "taCosts")
        log.Show(self.activityDatas.reAllInfo, 20, "reAllInfo")
        log.Show(self.activityDatas.taAllInfo, 20, "taAllInfo")
        log.Show(self.activityDatas.diffIndex, 20, "diffIndex")

    def do_checkData(self):
        """
        检查规划数据，设置WINDATA差异信息，以及主面板差异部分信息\n
        :return: 返回主面板展示差异部分信息
        """
        # 读取基础表输入框中涉及的类型表格和读取结果results
        # self.PrintData()
        results = self.activityDatas.searcher.sendOrders(self.activityDatas.allNumbers)

        if results:
            # 展示错误信息提示框并返回，不往下执行
            self.activityDatas.openDialog.showMessage(results)
            return

        # 规划和基础表内容比对 ------------------------------------------------
        taRewardsEdit = self.__checkContents(self.activityDatas.reRewards, self.activityDatas.taRewards,
                                             self.activityDatas.dirPath)  # 比对奖励内容是否一致
        length = len(taRewardsEdit)
        if self.activityDatas.isCosts:  # 如果有兑换消耗
            taCostsEdit = self.__checkContents(self.activityDatas.reCosts, self.activityDatas.taCosts,
                                               self.activityDatas.dirPath)  # 比对消耗是否一致
            for x in range(length):
                if self.activityDatas.taTitleDic["rewards"] < self.activityDatas.taTitleDic["costs"]:
                    taRewardsEdit[x].append(taCostsEdit[x])
                else:
                    taRewardsEdit[x].insert(0, taCostsEdit[x])

        for x in range(length):
            self.activityDatas.taAllInfo.append(taRewardsEdit[x])
            self.activityDatas.reAllInfo.append(self.activityDatas.reRewards[x])

        # self.PrintData()

    def __checkContents(self, reList: list, taList: list, dirPath: str) -> list:
        """
        将基础表和规划进行校验\n
        :param reList: 规划字符串列表
        :param taList: 基础表字符串列表
        :param dirPath: excel所在目录
        :return: 返回检查后的基础表列表，如果存在问题，会将问题进行标记
        """
        firstLength = len(reList)
        taListEdit = copy.deepcopy(taList)

        for ta_x in range(firstLength):
            # 获取二维列表的第一个列表，即数据的第一行 [[11,800029,20],[11,800030,20],...]
            taLine_2 = taList[ta_x]  # [['10', '1', '1'], ['10', '2', '1']]
            reLine_2 = reList[ta_x]  # [['元宝', '1'], ['金币', '1']]
            secondLength = len(reLine_2)  # 默认取规划列长为默认的遍历长度

            # 判断每一行的列长是否不一致
            if len(reLine_2) != len(taLine_2):
                offSet = len(reLine_2) - len(taLine_2)
                if offSet > 0:
                    self.activityDatas.colorKey = 1
                    secondLength = len(taLine_2)
                    info = self.activityDatas.mark.markT(f'规划比基础表多出{offSet}列')
                    taListEdit[ta_x].append(info)

                else:
                    # 如果规划列长减去基础表列长为负数，则基础表列长比规划列长的长度大
                    offSet = abs(offSet)  # 取偏移量的绝对值
                    for offset_y in range(offSet):
                        # 为偏移量直接做标记
                        self.activityDatas.colorKey = 1
                        taListEdit[ta_x][-(offset_y + 1)] = self.activityDatas.mark.markT(
                            f'[{",".join(taListEdit[ta_x][-(offset_y + 1)])}]')

            for ta_y in range(secondLength):
                # 获取数据的第一行的第一列，即第一行的第一个reward列表 ['11', '20177', '20'],
                taRewardItem_1 = taLine_2[ta_y]  # ['10', '1', '1']
                reRewardItem_1 = reLine_2[ta_y]  # ['元宝', '1']

                # 通过道具品类编码获取缓存数据
                reReader = self.activityDatas.searcher.getReader(dirPath, taRewardItem_1[0])
                if not reReader:
                    _name = "【无缓存数据】"
                else:
                    # 如果已经有缓存，则获取该ID的中文名称（该ID必须要有有中文名称）
                    _name = reReader.GetName(taRewardItem_1[1])
                    # 判断10和11的类型是否正确
                    if taRewardItem_1[0] == "10":
                        if not reReader.IsResource(taRewardItem_1[1]):
                            self.activityDatas.colorKey = 1
                            self.activityDatas.diffIndex.append(ta_x)
                            taListEdit[ta_x][ta_y][0] = self.activityDatas.mark.markT(taRewardItem_1[0])
                    if taRewardItem_1[0] == "11":
                        if reReader.IsResource(taRewardItem_1[1]):
                            self.activityDatas.colorKey = 1
                            self.activityDatas.diffIndex.append(ta_x)
                            taListEdit[ta_x][ta_y][0] = self.activityDatas.mark.markT(taRewardItem_1[0])

                # 将规划中的道具和基础表中的道具中文名称进行比较
                if _name != reRewardItem_1[0]:
                    self.activityDatas.colorKey = 1
                    # taListTable[ta_x][ta_y][1] = self.activityDatas.markT(f'{taRewardItem_1[1]},{_name}')
                    taListEdit[ta_x][ta_y][1] = self.activityDatas.mark.markT(f'{taRewardItem_1[1]},{_name}')
                    self.activityDatas.diffIndex.append(ta_x)

                # 将规划中的数量和基础表中的数量进行比较
                if taRewardItem_1[2] != reRewardItem_1[1]:
                    self.activityDatas.colorKey = 1
                    # taListTable[ta_x][ta_y][2] = self.activityDatas.markT(f'{taRewardItem_1[2]}')
                    taListEdit[ta_x][ta_y][2] = self.activityDatas.mark.markT(f'{taRewardItem_1[2]}')
                    self.activityDatas.diffIndex.append(ta_x)

        return taListEdit
